package it.polimi.TravelDream.ejb.searchManager;

import it.polimi.TravelDream.ejb.entities.Address;
import it.polimi.TravelDream.ejb.entities.Group;
import it.polimi.TravelDream.ejb.entities.Hotel;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import it.polimi.TravelDream.ejb.dtos.*;

/**
 * Session Bean implementation class HotelManager
 */
@Stateless
@LocalBean
public class HotelManager implements HotelMgr {
	
	@PersistenceContext
    private EntityManager em;
	private String name;
	private String city;
	
	private List<Hotel> hotelList = new ArrayList<Hotel>();
	private List<Address> addressList = new ArrayList<Address>();

    public HotelManager() {};
    
    @Override
    @RolesAllowed({Group._ADMIN})
    public void search(){
    	this.hotelList = em.createNamedQuery(Hotel.FIND_HOTELS_BY_NAME_AND_CITY, Hotel.class).setParameter("name", this.name).setParameter("city", this.city).getResultList();
    	for(Hotel h :this.hotelList){
    		this.addressList.add(h.getHotelAddress());
    	}
    }
    
    @Override
    @RolesAllowed({Group._ADMIN})
    public List <HotelDTO> getHotelResultList(List <AddressDTO> addressDTOs){
	   List <HotelDTO> hotelDTOs = new ArrayList <HotelDTO>();
   	
	   for(int i=0; i<this.hotelList.size(); i++){
		   HotelDTO hotelDTO = convertHotelToDTO (this.hotelList.get(i), addressDTOs.get(i));
		   hotelDTOs.add(hotelDTO);
	   }
	   
	   return hotelDTOs;
    }
    
    @Override
    @RolesAllowed({Group._ADMIN})
    public List <AddressDTO> getAddressResultList(){
 	   List <AddressDTO> addressDTOs = new ArrayList <AddressDTO>();
 	   	
 		   for (Address a : this.addressList){
 			  AddressDTO addressDTO = convertAddressToDTO(a);
 	   			addressDTOs.add(addressDTO);
 	   		}
 	   	return addressDTOs;
    }
   
    @Override
    @RolesAllowed({Group._ADMIN})
    public String getName() {
		return name;
	}
    
    @Override
    @RolesAllowed({Group._ADMIN})
	public void setName(String name) {
		this.name = name;
	}

	@Override
	@RolesAllowed({Group._ADMIN})
	public String getCity() {
		return city;
	}
	
	@Override
	@RolesAllowed({Group._ADMIN})
	public void setCity(String city) {
		this.city = city;
	}

	private HotelDTO convertHotelToDTO(Hotel h, AddressDTO addressDTO){
    	HotelDTO hotelDTO = new HotelDTO();
    	hotelDTO.setName(h.getName());
    	hotelDTO.setStars(h.getStars());
    	hotelDTO.setAddressDTO(addressDTO);
    	return hotelDTO;
    }
    
    private AddressDTO convertAddressToDTO (Address a){
    	AddressDTO addressDTO = new AddressDTO();
    	addressDTO.setStreet(a.getStreet());
    	addressDTO.setStreetNumber(a.getStreetNumber());
    	addressDTO.setCity(a.getCity());
    	addressDTO.setCap(a.getCap());
    	addressDTO.setDistrict(a.getDistrict());
    	addressDTO.setNation(a.getNation());
    	return addressDTO;
    }  
    
    @Override
    public void searchAll() {
    	this.hotelList = em.createNamedQuery(Hotel.FIND_ALL_HOTEL, Hotel.class).getResultList();
    	for(Hotel h :this.hotelList){
    		this.addressList.add(h.getHotelAddress());
    	}
    }
    
    @Override
    public void update(HotelDTO hotelDTO, int index){
    	Hotel hotel = new Hotel();
    	Address address = new Address();
    	
    	hotel = copyValuesHotelFromDTO(hotelDTO, index);
    	address = copyValuesAddressFromDTO(hotelDTO, index);
    	
    	hotel.setHotelAddress(address);
    	
    	em.merge(hotel);
    	em.merge(address);
    	
    }
    
    private Hotel copyValuesHotelFromDTO (HotelDTO hotelDTO, int index) {
    	Hotel hotel = new Hotel();
    	
    	hotel = hotelList.get(index);
    	
    	hotel.setName(hotelDTO.getName());
    	hotelDTO.setStars(hotelDTO.getStars());
    	
    	return hotel;
    	
    }
    
    private Address copyValuesAddressFromDTO (HotelDTO hotelDTO, int index) {
    	Address address = new Address();
    	AddressDTO addressDTO = hotelDTO.getAddressDTO();
    	
    	address = addressList.get(index);
    	
    	address.setStreet(addressDTO.getStreet());
    	address.setStreetNumber(addressDTO.getStreetNumber());
    	address.setCity(addressDTO.getCity());
    	address.setCap(addressDTO.getCap());
    	address.setDistrict(addressDTO.getDistrict());
    	address.setNation(addressDTO.getNation());
    	return address;
    }
 } 
    
    
    	
  
    


